package com.echo.yixiaowu.main

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.statusBars
import androidx.compose.ui.platform.LocalDensity
import androidx.navigation.compose.rememberNavController
import com.echo.yixiaowu.ui.theme.YixiaowuTheme
import com.echo.yixiaowu.util.SystemUtil

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            YixiaowuTheme {
                SystemUtil.init(
                    WindowInsets.statusBars.getTop(LocalDensity.current),
                    WindowInsets.navigationBars.getBottom(LocalDensity.current)
                )
                val navController = rememberNavController()
                MainContent(
                    navController = navController
                )
            }
        }
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
    }
}

// 获取当前 Activity
fun Context.findActivity(): Activity? = when (this) {
    is Activity -> this
    is ContextWrapper -> baseContext.findActivity()
    else -> null
}